// 用来显示班级人员信息的Grid(老师用)
var classesMemberGrid = Ext.create('Ext.grid.Panel', {
    store: classesMemberGridStore,
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '昵称',
            flex: 1,
            dataIndex: 'name',
            sortable: false
        }, {
            text: '所属小组',
            dataIndex: 'groupName',
            flex: 1,
            sortable: false
        }, {
            text: '操作',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 90,
            items: [{
                icon: 'static/images/icons/fam/look.ico',
                tooltip: '查看成绩',
                handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                    classesSeeGradeClick(record.data.classesId, record.data.id);
                }
            }]
        }
    ]
});

//用来显示班级人员信息的Grid(主任用)
var classesMemberGridForDirector = Ext.create('Ext.grid.Panel', {
    store: classesMemberGridStore,
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '昵称',
            flex: 1,
            dataIndex: 'name',
            sortable: false
        }, {
            text: '所属小组',
            dataIndex: 'groupName',
            flex: 1,
            sortable: false
        }, {
            text: '操作',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 90,
            items: [{
                icon: 'static/images/icons/fam/look.ico',
                tooltip: '查看成绩',
                handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                    classesSeeGradeClick(record.data.classesId, record.data.id);
                }
            }]
        }
    ],
    dockedItems: [{
        xtype: 'toolbar',
        items: [{
            text: '添加',
            tooltip: '添加班级成员',
            itemId: 'addButton',
            icon: 'static/images/icons/fam/add.gif',
            listeners: [{
                click: function () {
                    chooseClassesMemberAddGridStore.reload();
                    chooseClassesMemberAddGridCheckBox.deselectAll();
                    chooseClassesMemberAddGridWin.show();
                }
            }]
        }, '-', {
            text: '删除',
            tooltip: '删除班级成员',
            itemId: 'deleteButton',
            icon: 'static/images/icons/fam/delete.gif',
            listeners: [{
                click: function () {
                    var selectArray = classesMemberGridCheckBox.getSelection();
                    if (selectArray.length == 0)
                        Ext.Msg.alert("错误", "请选择要删除的班级成员员！");
                    else
                        classesMemberGridStore.remove(selectArray);
                }
            }]
        }]
    }],
    selModel: classesMemberGridCheckBox
});

//用来显示班级人员信息的成绩信息的Grid
var groupMemberGradeGrid = Ext.create('Ext.grid.Panel', {
    title: '分数详情',
    store: classesGradeStore,
    columns: [
        {
            text: '评分块',
            width: 100,
            dataIndex: 'part',
            flex: 1,
            sortable: false
        }, {
            text: '分数',
            dataIndex: 'grade',
            sortable: false,
            editor: {
                completeOnEnter: true,//是否允许用回车结束输入
                field: {
                    xtype: 'numberfield',
                    allowBlank: false,
                    maxValue: 100,
                    minValue: 0
                }
            }
        }, {
            text: '占比',
            dataIndex: 'percent',
            sortable: false,
            renderer: function (v, m, r) {
                return v + '%';
            }
        }
    ],
    features: [{
        id: 'group',
        ftype: 'groupingsummary',
        groupHeaderTpl: new Ext.XTemplate(
            '成绩所属项目: {[values.name.substring(32)]}'
        ),
        hideGroupedHeader: false,
        enableGroupingMenu: false
    }],
    plugins: {
        cellediting: {
            id: 'editorPlugins',
            clicksToEdit: 1,
            listeners: [{
                beforeEdit: function (editor, context, eOpts) {
                    var data = context.record.data;
                    if (data.part != '团队分数' && data.part != '总分')
                        return true;
                    else {
                        Ext.Msg.alert("错误", "该行不能修改分数！");
                        return false;
                    }
                }
            }, {
                edit: function (editor, context, eOpts) {
                    Ext.MessageBox.confirm('修改', '是否要将该成绩修改结果重新结算', function (btn) {
                        if (btn === 'yes') {
                            var data = context.record.data;
                            var gradeArray = classesGradeStore.queryRecords('projectId', data.projectId);
                            console.log(gradeArray);
                            var object = new Object();
                            for (var i = 0, length = gradeArray.length; i < length; i++) {
                                if (gradeArray[i].data.part == '测试需求')
                                    object['trgGrade'] = gradeArray[i].data.grade;
                                else if (gradeArray[i].data.part == '测试计划')
                                    object['designGrade'] = gradeArray[i].data.grade;
                                else if (gradeArray[i].data.part == '测试执行')
                                    object['runGrade'] = gradeArray[i].data.grade;
                                // else if (gradeArray[i].data.part == '测试报告')
                                //     object['reportGrade'] = gradeArray[i].data.grade;
                                else if (gradeArray[i].data.part == '考勤')
                                    object['checkGrade'] = gradeArray[i].data.grade;
                                else if (gradeArray[i].data.part != '个人分数' && gradeArray[i].data.part != '团队分数' && gradeArray[i].data.part != '总分')
                                    object['otherGrade'] = gradeArray[i].data.grade;
                            }
                            object['userId'] = groupMemberGradeGrid.userId;
                            object['project'] = data.projectId.substring(0, 32);
                            Ext.Ajax.request({
                                url: zuulUrl + '/' + testServiceName + '/testProject/manualSettleGrade',
                                method: 'post',
                                async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                                defaultXdrContentType: 'application/json',
                                jsonData: JSON.stringify(object),
                                success: function (response, opts) {
                                    Ext.Msg.alert("Success", "修改成功");
                                    classesGradeStore.reload();
                                },
                                failure: function (response, opts) {
                                    var obj = Ext.decode(response.responseText);
                                    if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                                        Ext.Msg.alert("错误", obj.message);
                                    else
                                        Ext.Msg.alert("错误", "该功能暂时不可用！");
                                }
                            });
                        }
                    });
                }
            }]
        }
    },
    buttons: [{
        text: '返回',
        listeners: [{
            click: function () {
                Ext.getCmp('centerRegion').removeAll(false);
                if (classesNowDepth == 1) {
                    classesTabPanel.setActiveTab(0);
                    Ext.getCmp('centerRegion').add(classesTabPanel);
                } else if (classesNowDepth == 2) {
                    groupTabPanel.setActiveTab(0);
                    Ext.getCmp('centerRegion').add(groupTabPanel);
                }
            }
        }]
    }]
});

//用于显示组人员信息的grid表单(老师用)
var groupMemberGrid = Ext.create('Ext.grid.Panel', {
    store: groupMemberGridStore,
    dockedItems: [{
        xtype: 'toolbar',
        items: [{
            text: '添加',
            tooltip: '添加组成员',
            itemId: 'addButton',
            icon: 'static/images/icons/fam/add.gif',
            listeners: [{
                click: function () {
                    var selectArray = classesLeftTreePanel.getSelectionModel().getSelected();// 得到被选择的行的数组.
                    var classesId = selectArray.items[0].data.parentId;
                    chooseClassesMemberAddGroupGridStore.proxy.url = zuulUrl + '/' + authServiceName + '/classes/members/' + classesId;
                    chooseClassesMemberAddGroupGridStore.reload();
                    chooseClassesMemberCheckBox.deselectAll();
                    chooseClassesMemberAddGroupGridWin.show();
                }
            }]
        }, '-', {
            text: '删除',
            tooltip: '删除组成员',
            itemId: 'deleteButton',
            icon: 'static/images/icons/fam/delete.gif',
            listeners: [{
                click: function () {
                    var selectArray = groupMemberGridCheckBox.getSelection();
                    if (selectArray.length == 0)
                        Ext.Msg.alert("错误", "请选择要删除的组员！");
                    else
                        groupMemberGridStore.remove(selectArray);
                }
            }]
        }]
    }],
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '昵称',
            dataIndex: 'userName',
            flex: 1,
            sortable: false
        }, {
            text: '身份',
            dataIndex: 'isLeader',
            sortable: false,
            editor: {
                field: {
                    xtype: 'combo',
                    editable: false,//防止用户直接在下拉框里输入
                    store: groupMemberIsLeaderComboGridStore,
                    queryMode: 'local',
                    displayField: 'isLeaderName',
                    valueField: 'isLeaderNum',
                    allowBlank: false//是否允许为空
                }
            },
            renderer: function (v, m, r) {
                return v == 1 ? '组长' : '组员';
            }
        }, {
            text: '操作',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 90,
            items: [{
                icon: 'static/images/icons/fam/look.ico',
                tooltip: '查看成绩',
                handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                    groupSeeGradeClick(record.data.groupId, record.data.userId);
                }
            }]
        }
    ],
    selModel: groupMemberGridCheckBox,
    plugins: {
        cellediting: {
            clicksToEdit: 1
        }
    }
});

//用于显示组人员信息的grid表单(主任用)
var groupMemberGridForDirector = Ext.create('Ext.grid.Panel', {
    store: groupMemberGridStore,
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '昵称',
            dataIndex: 'userName',
            flex: 1,
            sortable: false
        }, {
            text: '身份',
            dataIndex: 'isLeader',
            sortable: false,
            renderer: function (v, m, r) {
                return v == 1 ? '组长' : '组员';
            }
        }, {
            text: '操作',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 90,
            items: [{
                icon: 'static/images/icons/fam/look.ico',
                tooltip: '查看成绩',
                handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                    groupSeeGradeClick(record.data.groupId, record.data.userId);
                }
            }]
        }
    ]
});

//用于给添加组员选择班级成员的grid
var chooseClassesMemberAddGroupGrid = Ext.create('Ext.grid.Panel', {
    store: chooseClassesMemberAddGroupGridStore,
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '昵称',
            dataIndex: 'name',
            sortable: false
        }, {
            text: '所属组',
            flex: 1,
            dataIndex: 'groupName',
            sortable: false
        }
    ],
    selModel: chooseClassesMemberCheckBox
});

//用于给添加班级成员选择班级成员的grid
var chooseClassesMemberAddGrid = Ext.create('Ext.grid.Panel', {
    store: chooseClassesMemberAddGridStore,
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '昵称',
            flex: 1,
            dataIndex: 'realName',
            sortable: false
        }, {
			text: '用户名',
			flex: 1,
			dataIndex: 'userName',
			sortable: false
		}, {
            text: '已属班级数量',
            dataIndex: 'hasClassesCount'
        }
    ],
    selModel: chooseClassesMemberAddGridCheckBox
});

//用于显示复制测试需求的审核记录Grid
var testReqCopyCheckLogGrid = Ext.create('Ext.grid.Panel', {
    store: testCopyCheckLogStore,
    columns: [new Ext.grid.RowNumberer(),//显示行号
        {
            text: '状态',
            width: 100,
            renderer: function (v, record) {
                if (record.record.data.isFinish == 1) {
                    if (record.record.data.isPass == 1)
                        return '审核通过';
                    else
                        return '审核不通过';
                } else
                    return '审核中';
            },
        }, {
            text: '审核人',
            dataIndex: 'checkMan',
            flex: 1
        }, {
            text: '审核日期',
            dataIndex: 'checkDate',
            flex: 0.5,
            renderer: function (value) {
                return value.replace('T', '  ');
            }
        }, {
            text: '查看批注',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 100,
            items: [{
                icon: 'static/images/icons/fam/look.ico',
                tooltip: '查看批注',
                handler: function (grid, rowIndex, colIndex) {
                    var id = testCopyCheckLogStore.getAt(rowIndex).data.id;
                    Ext.Ajax.request({//然后去后台请求该审核记录的批注内容
                        url: zuulUrl + '/' + testServiceName + '/testProject/copyTrqCheckLogAnnotate/' + id,
                        async: true,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                        success: function (response, opts) {
                            testCopyCheckAnnotatePanel.setHtml('<p><b>' + response.responseText + '</b></p>');
                            testCopyChecklogAnnotateWin.show();
                        },
                        failure: function (response, opts) {
                            var obj = Ext.decode(response.responseText);
                            if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                                Ext.Msg.alert("错误", obj.message);
                            else
                                Ext.Msg.alert("错误", "该功能暂时不可用！");
                        }
                    });
                }
            }]
        }]
});

//用于显示复制的测试计划的测试步骤的grid
var testDesignCopyStepGrid = Ext.create('Ext.grid.Panel', {
    store: testDesignStepCopyStore,
    columns: [new Ext.grid.RowNumberer(),//显示行号
        {
            text: '名称',
            width: 100,
            dataIndex: 'name',
            sortable: false
        }, {
            text: '内容',
            dataIndex: 'description',
            flex: 1,
            sortable: false,
            renderer: function (v, m, r) {
                if (v.length < 15)
                    return v;
                else
                    return '内容过长,请展开查看';
            }
        }, {
            text: '期望结果',
            dataIndex: 'expectedResult',
            flex: 0.5,
            sortable: false
        }],
    plugins: {
        rowexpander: {
            rowBodyTpl: new Ext.XTemplate(
                '{description}'
            )
        }
    }
});

//用于显示测试计划的关联测试需求的复制的grid
var testDesignTestRequirementCopyGrid = Ext.create('Ext.grid.Panel', {
    store: testDesignTestRequirmentCopyStore,
    columns: [new Ext.grid.RowNumberer(),//显示行号
        {
            text: '名称',
            width: 100,
            dataIndex: 'title'
        }, {
            text: '编写者',
            dataIndex: 'author',
            flex: 0.5
        }, {
            text: '内容',
            dataIndex: 'description',
            flex: 1,
            renderer: function (v, m, r) {
                if (v.length < 15)
                    return v;
                else
                    return '内容过长,请展开查看';
            }
        }],
    plugins: {
        rowexpander: {
            rowBodyTpl: new Ext.XTemplate(
                '{description}'
            )
        }
    }
});

//用于显示测试计划的复制审核记录grid
var testDesignCheckLogCopyGrid = Ext.create('Ext.grid.Panel', {
    store: testCopyCheckLogStore,
    columns: [new Ext.grid.RowNumberer(),//显示行号
        {
            text: '状态',
            width: 100,
            renderer: function (v, record) {
                if (record.record.data.isFinish == 1) {
                    if (record.record.data.isPass == 1)
                        return '审核通过';
                    else
                        return '审核不通过';
                } else
                    return '审核中';
            },
        }, {
            text: '审核人',
            dataIndex: 'checkMan',
            flex: 0.5
        }, {
            text: '审核日期',
            dataIndex: 'checkDate',
            flex: 0.5,
            renderer: function (value) {
                return value.replace('T', '  ');
            }
        }, {
            text: '查看批注',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 100,
            items: [{
                icon: 'static/images/icons/fam/look.ico',
                tooltip: '查看批注',
                handler: function (grid, rowIndex, colIndex) {
                    var id = testCopyCheckLogStore.getAt(rowIndex).data.id;
                    Ext.Ajax.request({//然后去后台请求该审核记录的批注内容
                        url: zuulUrl + '/' + testServiceName + '/testProject/copyDesignCheckLogAnnotate/' + id,
                        async: true,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                        success: function (response, opts) {
                            testCopyCheckAnnotatePanel.setHtml('<p><b>' + response.responseText + '</b></p>');
                            testCopyChecklogAnnotateWin.show();
                        },
                        failure: function (response, opts) {
                            var obj = Ext.decode(response.responseText);
                            if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                                Ext.Msg.alert("错误", obj.message);
                            else
                                Ext.Msg.alert("错误", "该功能暂时不可用！");
                        }
                    });
                }
            }]
        }]
});

//用于显示复制测试执行记录的grid
var testRunCopyLogGrid = Ext.create('Ext.grid.Panel', {
    alias: 'widget.MainGrid',
    header: false,
    height:'60%',
    border: false,
    store: testRunlogRowWidGetStore,
    columns: [new Ext.grid.RowNumberer(), {
        text: "测试机",
        dataIndex: 'host',
        sortable: false
    }, {
        text: "状态",
        dataIndex: 'status',
        sortable: false,
        renderer: function (value) {
            if (value == -1)
                return "<img src='static/images/icons/fam/warning.ico' style='vertical-align:text-top;'  />未完成";
            else if (value == 0)
                return "<img src='static/images/icons/fam/cross.gif' style='vertical-align:text-top;'  /> 失败";
            else
                return "<img src='static/images/icons/fam/accept.png' style='vertical-align:text-top;'  />成功";
        },
    }, {
        text: "执行时长",
        sortable: false,
        dataIndex: 'duration',
    }, {
        text: "浏览器版本",
        sortable: false,
        dataIndex: 'browserVersion',
    }, {
        text: "操作系统",
        sortable: false,
        dataIndex: 'os',
    }, {
        text: "创建时间",
        sortable: false,
        dataIndex: 'createDate',
        flex: 0.5,
        renderer: function (value) {
            if (value == null)
                return '无';
            else
                return value.replace('T', '  ');
        }
    }],
    selModel: {
        selType: 'cellmodel'
    },
    plugins: [{
        ptype: 'rowwidget',
        widget: {
            xtype: 'grid',
            bind: {
                store: '{record.testRunLogStep}',
            },
            header: false,
            selModel: {
                selType: 'cellmodel'
            },
            columns: [new Ext.grid.RowNumberer(),
                {
                    text: "步骤名称",
                    dataIndex: 'name',
                    flex: 0.5,
                    sortable: false,
                }, {
                    text: "状态",
                    dataIndex: 'isPass',
                    sortable: false,
                    renderer: function (value) {
                        if (value == -1)
                            return "<img src='static/images/icons/fam/warning.ico' style='vertical-align:text-top;'  />未完成";
                        else if (value == 0)
                            return "<img src='static/images/icons/fam/cross.gif' style='vertical-align:text-top;'  /> 失败";
                        else
                            return "<img src='static/images/icons/fam/accept.png' style='vertical-align:text-top;'  />成功";
                    }
                },
                {
                    text: "理由",
                    dataIndex: 'reason',
                    flex: 1,
                    sortable: false,
                    renderer: function (v, m, r) {
                        if (v == null)//不知道为什么如果显示的是null就无法触发点击事件
                            return '';
                        else if (v.length < 15)
                            return v;
                        else
                            return '内容过长,请展开查看';
                    }
                }
            ],
//			columnLines: true,
//			autoWidth: true,
//			autoHeight: true,
            // width: 400,renderId
            // height: 200,
            iconCls: 'icon-grid',
            plugins: {
                rowexpander: {
                    rowBodyTpl: new Ext.XTemplate(
                        '{reason}'
                    )
                }
            }
        }

    }],
    collapsible: true,
    animCollapse: false,
    iconCls: 'icon-grid',
    listeners: [{
        itemclick: function (view, record, item, index, e, eOpts) {//当选择对应的执行记录时，下面的测试步骤详情也要随之改变
            testRunLogStepRowWidGetStore.removeAll();
            testRunLogStepRowWidGetStore.setData(testDesignCopyBorderShot(record.data.testRunLogStep));
        }
    }]
});

//用于显示复制测试执行的测试步骤的详细信息grid
var testRunLogCopyStepGrid = Ext.create('Ext.grid.Panel', {
    store: testRunLogStepRowWidGetStore,
//    collapsible: true, // 有没有按钮向内缩进
//    collapseDirection:'left',
//    titleAlign:'right',
//    headerPosition:'right',
    //autoScroll: true, //大小溢出时自动出现滚动条
    height:'40%',
    header: {
        height: 23,
        padding: '2 0 2 10 '
    },
    title: '测试步骤信息',
    border: false,
    columns: [new Ext.grid.RowNumberer(),// 显示行号
        {
            text: '名称',
            width: 100,
            dataIndex: 'name',
            sortable: false
        }, {
            text: '内容',
            dataIndex: 'description',
            flex: 1,
            sortable: false,
            renderer: function (v, m, r) {
                if (v.length < 15)
                    return v;
                else
                    return '内容过长,请展开查看';
            }
        }, {
            text: '期望结果',
            dataIndex: 'expectedResult',
            flex: 0.5,
            sortable: false
        }],
    plugins: {
        rowexpander: {
            rowBodyTpl: new Ext.XTemplate(
                '{description}'
            )
        }
    }
});

//用于显示复制测试执行的审核记录
var testRunCheckLogCopyGrid = Ext.create('Ext.grid.Panel', {
    store: testCopyCheckLogStore,
    columns: [new Ext.grid.RowNumberer(),//显示行号
        {
            text: '状态',
            width: 100,
            renderer: function (v, record) {
                if (record.record.data.isFinish == 1) {
                    if (record.record.data.isPass == 1)
                        return '审核通过';
                    else
                        return '审核不通过';
                } else
                    return '审核中';
            },
        }, {
            text: '审核人',
            dataIndex: 'checkMan',
            flex: 0.5
        }, {
            text: '审核日期',
            dataIndex: 'checkDate',
            flex: 0.5,
            renderer: function (value) {
                return value.replace('T', '  ');
            }
        }, {
            text: '查看批注',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 100,
            items: [{
                icon: 'static/images/icons/fam/look.ico',
                tooltip: '查看批注',
                handler: function (grid, rowIndex, colIndex) {
                    var id = testCopyCheckLogStore.getAt(rowIndex).data.id;
                    Ext.Ajax.request({//然后去后台请求该审核记录的批注内容
                        url: zuulUrl + '/' + testServiceName + '/testProject/copyRunCheckLogAnnotate/' + id,
                        async: true,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                        success: function (response, opts) {
                            testCopyCheckAnnotatePanel.setHtml('<p><b>' + response.responseText + '</b></p>');
                            testCopyChecklogAnnotateWin.show();
                        },
                        failure: function (response, opts) {
                            var obj = Ext.decode(response.responseText);
                            if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                                Ext.Msg.alert("错误", obj.message);
                            else
                                Ext.Msg.alert("错误", "该功能暂时不可用！");
                        }
                    });
                }
            }]
        }]
});

//用于显示班级负责人的选择Grid
var classesChooseClassesLeaderGrid = Ext.create('Ext.grid.Panel', {
    header: false,
    store: classesChooseClassesLeaderGridStore,
    columns: [new Ext.grid.RowNumberer(), //显示行号
        {
            text: '名称',
            flex: 1,
            align: 'center',
            dataIndex: 'realName',
            hideable: false,
        }, {
            text: '负责的班级数量',
            flex: 1,
            align: 'center',
            dataIndex: 'hasClassesCount',
            hideable: false,
        }
    ],
    listeners: [{
        itemdblclick: function (view, record, item, index, e, eOpts) {
            classesDetailForm.down('#leader').setValue(record.data.realName);
            classesDetailForm.down('#leader').leaderId = record.data.id;
            classesChooseClassesLeaderGridWin.close();
        }
    }]
});